package com.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.entity.Info;

@Repository
public class InfoDAO extends BaseDAO<Info> {

	private final static String INSERT_TIME = "insertTime";
	
	public InfoDAO() {
		super(Info.class);
	}
	
	public List<Info> findByType(String typeName){
		String hql = "where o.infoType.typeName=? order by " + INSERT_TIME;
		return this.findResultList(hql, typeName);
	}
	
	public List<Info> findByType(String typeName, int start, int limit){
		String hql = "where o.infoType.typeName=? order by " + INSERT_TIME;
		return this.findResultListInPage(hql, start, limit, typeName);
	}
	
	public int findCountByType(String typeName){
		String hql = "where o.infoType.typeName=?";
		Object result = this.findResultCount(hql, typeName);
		if(result == null){
			return 0;
		}
		return ((Long) result).intValue();
	}
	
	public List<Info> findRelated(String[] tags){
		String hql = "where 1=2 ";
		for(int i = 1; i < tags.length; i++){
			hql = hql + "or o.tags like %," + tags[i] + ", ";
		}
		return this.findResultList(hql);
	}

}
